import { reactive, ref } from 'vue'
import http from '@/utils/request.js'
import { useI18n } from 'vue-i18n'
import { useValidations } from '@/hooks/useValidations'
export const useForm = () => {
  const { validateIdCard, validatePhone } = useValidations()
  const { t } = useI18n()

  const formData = reactive({
    // 金额
    loansAmount: '',
    // 勾选数据
    choice: [],
    // 姓名
    userName: '',
    // 通信公司
    applicationNo: 0,
    // 公司名称
    companyName: '',
    // 手机
    phoneNumber: '',
    // 地址
    addressNow: '',
    // 贷款
    isOtherLoans: '',
    // 保险
    isFourInsurance: '',
    // 月薪
    monthlySalary: '',
    // 职业
    occupation: '',
    // 资金用途
    loansUseId: '',
    // 税后年收入
    annualIncomeAfterTax: '',
    // 婚姻状况
    maritalStatus: '',
    // 紧急联系人姓名
    emergencyContactName: '',
    // 紧急联系人电话
    emergencyContactPhone: '',
    // 住房类型
    housingType: '',
    // 贷款期限(月)
    loanTerm: '',
    // 还款方式
    repaymentType: '',
    // 公司电话
    enterprisePhone: '',
    // 上年度营业收入
    annualRevenue: '',
    // 上年度纳税额
    annualTax: '',
    // 企业类型
    enterpriseType: '',
    // 营业执照图片
    businessLicense: '',
    // 营业执照号码
    businessLicenseNo: '',
    // 身份证正面
    idCardFront: '',
    // 手持居住证
    residencePermit: '',
    // 身份证号码
    idCardNumber: ''
  })

  /**
   * 第三步输入验证
   * @param {*} e 验证数据
   * @returns
   */
  const threeValidate = (e, isEnterprise) => {
    if (!e.userName) {
      uni.showToast({
        title: t('apply.name.placeholder'),
        icon: 'none'
      })
      return true
    }

    if (validateIdCard(e.idCardNumber)) return true

    if (e.applicationNo === '') {
      uni.showToast({
        title: t('apply.telecom.placeholder'),
        icon: 'none'
      })
      return true
    }

    if (validatePhone(e.phoneNumber)) return true

    return false
  }

  /**
   * 第四步输入验证
   * @param {*} e 验证数据
   * @returns
   */
  const fourValidate = (e) => {
    if (e.idCardFront.length === 0) {
      uni.showToast({
        title: t('bank.name.7'),
        icon: 'none'
      })
      return true
    }
    return false
  }

  /**
   * 第五步输入验证
   * @param {*} e 验证数据
   * @returns
   */
  const fiveValidate = (e) => {
    if (e.residencePermit.length === 0) {
      uni.showToast({
        title: t('enterprise.id.title2.comment'),
        icon: 'none'
      })
      return true
    }
    return false
  }

  /**
   * 第六步输入验证
   * @param {*} e 验证数据
   * @returns
   */
  const sixValidate = (e, isEnterprise) => {
    if (isEnterprise) {
      if (!e.addressNow) {
        uni.showToast({
          title: t('enterprise.address.comment'),
          icon: 'none'
        })
        return true
      }

      if (!e.companyName) {
        uni.showToast({
          title: t('enterprise.name2.comment'),
          icon: 'none'
        })
        return true
      }

      if (validatePhone(e.enterprisePhone, 1)) return true

      if (!e.annualRevenue) {
        uni.showToast({
          title: t('enterprise.income.comment'),
          icon: 'none'
        })
        return true
      }

      if (!e.annualTax) {
        uni.showToast({
          title: t('enterprise.tax.comment'),
          icon: 'none'
        })
        return true
      }

      if (e.enterpriseType === '') {
        uni.showToast({
          title: t('enterprise.type.comment'),
          icon: 'none'
        })
        return true
      }

      if (e.businessLicenseNo === '') {
        uni.showToast({
          title: t('enterprise.id.title5.comment'),
          icon: 'none'
        })
        return true
      }
    } else {
      if (e.housingType === '') {
        uni.showToast({
          title: t('apply.house.type.placeholder'),
          icon: 'none'
        })
        return true
      }
      if (!e.addressNow) {
        uni.showToast({
          title: t('apply.region.placeholder'),
          icon: 'none'
        })
        return true
      }

      if (e.occupation === '') {
        uni.showToast({
          title: t('apply.occupation.placeholder'),
          icon: 'none'
        })
        return true
      }
      if (!e.monthlySalary) {
        uni.showToast({
          title: t('apply.salary.placeholder'),
          icon: 'none'
        })
        return true
      }

      if (!e.maritalStatus) {
        uni.showToast({
          title: t('apply.marriage.placeholder'),
          icon: 'none'
        })
        return true
      }

      if (e.emergencyContactName === '') {
        uni.showToast({
          title: t('apply.emergency.name.placeholder'),
          icon: 'none'
        })
        return true
      }
      if (validatePhone(e.emergencyContactPhone)) return true
    }

    return false
  }

  /**
   * 第七步输入验证
   * @param {*} e 验证数据
   * @returns
   */

  const sevenValidate = (e, isEnterprise) => {
    if (isEnterprise) {
      if (e.businessLicense === '') {
        uni.showToast({
          title: t('enterprise.id.title4.comment'),
          icon: 'none'
        })
        return true
      }
    } else {
      if (e.isOtherLoans === '') {
        uni.showToast({
          title: t('apply.loan.placeholder'),
          icon: 'none'
        })
        return true
      }
      if (e.isFourInsurance === '') {
        uni.showToast({
          title: t('apply.insurance.placeholder'),
          icon: 'none'
        })
        return true
      }

      if (e.loansUseId === '') {
        uni.showToast({
          title: t('apply.purpose.placeholder'),
          icon: 'none'
        })
        return true
      }

      if (e.repaymentType === '') {
        uni.showToast({
          title: t('apply.repayment.placeholder'),
          icon: 'none'
        })
        return true
      }
    }

    return false
  }

  /**
   * 第八步输入验证
   * @param {*} e 验证数据
   * @returns
   */

  const eightValidate = (e) => {
    if (e.loansUseId === '') {
      uni.showToast({
        title: t('apply.purpose.placeholder'),
        icon: 'none'
      })
      return
    }

    if (e.repaymentType === '') {
      uni.showToast({
        title: t('apply.repayment.placeholder'),
        icon: 'none'
      })
      return
    }
    if (e.emergencyContactName === '') {
      uni.showToast({
        title: t('apply.emergency.name.placeholder'),
        icon: 'none'
      })
      return
    }
    if (validatePhone(e.emergencyContactPhone)) return
    return true
  }

  /**
   * 个人申请验证
   * @param {*} e 验证数据
   * @returns
   */
  const validate = (e) => {
    if (!e.userName) {
      uni.showToast({
        title: t('apply.name.placeholder'),
        icon: 'none'
      })
      return
    }

    if (e.idCardNumber === '') {
      uni.showToast({
        title: t('bank.name.25'),
        icon: 'none'
      })
      return
    }

    if (!e.phoneNumber) {
      uni.showToast({
        title: t('apply.phone.placeholder'),
        icon: 'none'
      })
      return
    }
    if (!e.addressNow) {
      uni.showToast({
        title: t('apply.region.placeholder'),
        icon: 'none'
      })
      return
    }
    if (e.isOtherLoans === '') {
      uni.showToast({
        title: t('apply.loan.placeholder'),
        icon: 'none'
      })
      return
    }
    if (e.isFourInsurance === '') {
      uni.showToast({
        title: t('apply.insurance.placeholder'),
        icon: 'none'
      })
      return
    }
    if (!e.monthlySalary) {
      uni.showToast({
        title: t('apply.salary.placeholder'),
        icon: 'none'
      })
      return
    }

    if (e.occupation === '') {
      uni.showToast({
        title: t('apply.occupation.placeholder'),
        icon: 'none'
      })
      return
    }
    if (e.loansUseId === '') {
      uni.showToast({
        title: t('apply.purpose.placeholder'),
        icon: 'none'
      })
      return
    }

    if (!e.maritalStatus) {
      uni.showToast({
        title: t('apply.marriage.placeholder'),
        icon: 'none'
      })
      return
    }

    if (e.emergencyContactName === '') {
      uni.showToast({
        title: t('apply.emergency.name.placeholder'),
        icon: 'none'
      })
      return
    }

    if (validatePhone(e.emergencyContactPhone)) return true

    if (e.houseType === '') {
      uni.showToast({
        title: t('apply.house.type.placeholder'),
        icon: 'none'
      })
      return
    }

    if (e.repaymentType === '') {
      uni.showToast({
        title: t('apply.repayment.placeholder'),
        icon: 'none'
      })
      return
    }

    if (e.idCardFront.length === 0) {
      uni.showToast({
        title: t('bank.name.7'),
        icon: 'none'
      })
      return
    }

    return true
  }

  /**
   * 提交
   * @param {*} data 提交数据
   * @param {boolean} isEnterprise 是否是公司：true 是公司申请贷款 false 个人申请贷款
   * @returns
   */
  const onSubmit = async (data, isEnterprise) => {
    // 待提交材料
    if ([0, 4].includes(data.status)) {
      if (isEnterprise ? !eightValidate(formData) : !validate(formData)) return

      try {
        uni.showLoading({
          title: t('loading.title'),
          mask: true // 防止触摸穿透
        })
        const e = formData
        let upData = isEnterprise
          ? {
              loanAmount: e.loansAmount,
              legalName: e.userName,
              legalPhone: e.phoneNumber,
              enterpriseAddress: e.addressNow,
              enterpriseName: e.companyName,
              enterprisePhone: e.enterprisePhone,
              annualRevenue: e.annualRevenue,
              annualTax: e.annualTax,
              enterpriseType: e.enterpriseType,
              loansUseId: e.loansUseId,
              repaymentType: e.repaymentType == 2 ? 3 : e.repaymentType,
              businessLicenseNo: e.businessLicenseNo,
              bankStatementImage: e.businessLicense,
              idCardFrontImage: e.idCardFront,
              // residencePermitPhoto: e.residencePermit,
              residentId: e.idCardNumber,
              applicationNo: e.applicationNo,
              emergencyContactName: e.emergencyContactName,
              emergencyContactPhone: e.emergencyContactPhone,
              status: 1
            }
          : {
              loansAmount: e.loansAmount,
              userName: e.userName,
              phoneNumber: e.phoneNumber,
              addressNow: e.addressNow,
              isOtherLoans: e.isOtherLoans,
              isFourInsurance: e.isFourInsurance,
              monthlySalary: e.monthlySalary,
              occupation: e.occupation,
              loansUseId: e.loansUseId,
              maritalStatus: e.maritalStatus == 3 ? 4 : e.maritalStatus,
              emergencyContactName: e.emergencyContactName,
              emergencyContactPhone: e.emergencyContactPhone,
              housingType: e.housingType,
              repaymentType: e.repaymentType == 2 ? 3 : e.repaymentType,
              idCardFrontImage: e.idCardFront,
              // residencePermitPhoto: e.residencePermit,
              idCardNumber: e.idCardNumber,
              applicationNo: e.applicationNo,
              status: 1
              // photoList: [data.url]
            }

        await http.post(
          isEnterprise ? '/api/service/loansCompanse' : '/api/service/loansApplication',
          upData
        )

        uni.reLaunch({
          url: `/pages/success-page/success-page?state=1${isEnterprise ? '&type=enterprise' : ''}`
        })
      } catch (err) {
        uni.hideLoading()
      }
    }

    // uni.uploadFile({
    //   url: 'http://tdba649a.natappfree.cc/common/upload', //仅为示例，非真实的接口地址
    //   // files: imageValue.value,
    //   filePath: imageValue.value[0],
    //   success: async (res) => {
    //     const data = JSON.parse(res.data)
    //   }
    // })
  }

  return {
    formData,
    onSubmit,
    threeValidate,
    fourValidate,
    fiveValidate,
    sixValidate,
    sevenValidate,
    eightValidate
  }
}
